﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Entities;
using CoVanHocTapData.Infrastructure;

namespace CoVanHocTapData.Concretes
{
    public class NganhDaoTaoRepository : INganhRepository
    {
        DangKyPhuEntities entities;
        
        public NganhDaoTaoRepository()
        {
            entities = new DangKyPhuEntities();
        }

        #region Repository
        SinhVienRepository sinhVienRepo = new SinhVienRepository();
        #endregion

        #region INganhRepository Members

        public IQueryable<NganhDaoTao> LayNganhDaoTao()
        {
            return (from m in entities.NganhDaoTaos
                    select m);
        }

        public IQueryable<NganhDaoTao> LayNganhDaoTao(string KhoaID)
        {
            return (from m in entities.NganhDaoTaos
                    where m.Khoa.KhoaID.Equals(KhoaID)
                    select m);
        }

        public IQueryable<NganhDaoTao> LayNganhDaoTaoTheoNganh(string nganhID)
        {
            return (from m in entities.NganhDaoTaos
                    where m.NganhID.Equals(nganhID)
                    select m);
        }


        public string LayMaNganh(string LopID)
        {
            return (from m in entities.Lops
                    where m.LopID.Equals(LopID)
                    select m.NganhDaoTao.NganhID).ToList().FirstOrDefault();
        }

        public NganhDaoTao LayNganhTuMaSV(string maSV)
        {
            SinhVien sinhVien = sinhVienRepo.LaySVTuMaSV(maSV);            
            return (from m in entities.NganhDaoTaos
                    where m.NganhID == sinhVien.Lop.NganhID
                    select m).FirstOrDefault();
        }



        #endregion

        public IQueryable<NganhDaoTao> LayNganhDaoTaoChuyenNganh2TheoSinhVien(SinhVien sv)
        {
            return (from m in entities.NganhDaoTaos
                        join n in entities.NganhDaoTao2
                            on m.NganhID equals n.NganhID
                        join l in entities.SinhVienKhois
                            on n.MaKhoi equals l.KhoiID
                        where l.MSSV.Equals(sv.MSSV) && n.He.Equals(sv.Lop.HeDaoTaoID)
                        select m);
        }

        public NganhDaoTao LayNganh2CuaSinhVien(string maSV)
        {
            var nganh = new NganhDaoTao();
            var maNganh2 = new NganhHoc2Repository().NganhHoc2CuaSinhVien(maSV);
            if (maNganh2 != null)
            {
                nganh = (from m in entities.NganhDaoTaos
                             where m.NganhID == maNganh2
                             select m).FirstOrDefault();
            }
            return nganh;
        }




        public string LayMaNganhTuMaSV(string maSV)
        {
            var maNganh = (from m in entities.NganhDaoTaos
                           join n in entities.Lops
                           on m.NganhID equals n.NganhID
                           join o in entities.SinhViens
                           on n.LopID equals o.LopID
                           where o.MSSV == maSV
                           select m.NganhID).FirstOrDefault();
            return maNganh;
        }
    }
}
